/* GMServer.hh -- Servidor
 *
 * Copyright (C) 2008, Diogo F. S. Ramos <diogo.ramos@usp.br>
 * GEAR - Grupo de Estudos Avançados em Robótica
 * <http://www.sel.eesc.usp.br/gear>
 *
 * This file is part of GEARMedula.
 * 
 * GEARMedula is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, version 3 of the License.
 * 
 * GEARMedula is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#ifndef GMSERVER_HH_
#define GMSERVER_HH_

#include <sys/types.h>

#include "XError.hh"
#include "Monitor.hh"
#include "Control.hh"
#include "Thread.hh"
#include "Services.hh"
#include "RPCTranslator.hh"

class GMServer : public Thread
{
private:
    int socketfd_;
    XError xerror_;
    fd_set active_fd_set_;
    fd_set read_fd_set_;
    RPCTranslator rpc_translator_;
    char received_data_buffer_[ kBufferSize ];
    char sended_data_buffer_[ kBufferSize ];
    
    Services services_;

    void CreateSocket( unsigned int port );
    int ReadAndDecodeMessage(unsigned int socket_in );
    void Run( void * ptr );

    int SendMessage( unsigned int socket_in );
    
public:
    GMServer(void);
    ~GMServer(void);
    void Init( unsigned int port, Monitor * monitor, Control * control );
};

#endif	// GMSERVER_HH_
